package com.nulp.psycho.web.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.nulp.psycho.db.model.Question;
import com.nulp.psycho.db.model.Test;
import com.nulp.psycho.db.service.QuestionService;
import com.nulp.psycho.db.service.TestService;

@WebServlet("/GetResultServlet")
public class GetResultServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public GetResultServlet() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {		
		int testId = Integer.parseInt(request.getParameter("testIdid"));
		TestService tService = new TestService();
		Test test = tService.read(testId);		
		List<Question> questions = new QuestionService().getAllQuestionsByTestId(testId);
		List<String> ans = new ArrayList<String>();
		for (int i = 0; i < questions.size(); i++) {
			ans.add(request.getParameter("answerRadio" + i));
		}
		
		
		request.setAttribute("answers", ans);
		request.getRequestDispatcher(test.getUrl()).forward(request, response);
	}

}
